
//客服接口

package public

import (
    "fmt"
	"errors"
	"wxsdk/miniapp/core"
	"github.com/astaxie/beego"
)
var (
	customerURL = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=%s" 
)

// 发文本消息
func  SendTextMessage(authorizerAccessToken, touser, content string)(string,error){
	serverUrl := fmt.Sprintf(customerURL, authorizerAccessToken)
	client := core.GetPostJSONClient()
	text := NewText(touser, content, "")
	var rsp core.ErrorResponse
	tbdata, err := client.Excute("POST", serverUrl, text, &rsp)
    if err != nil {
		beego.Critical(err.Error())
		return "", err
	}
	if rsp.ErrCode != 0 {
		beego.Critical("error:", string(tbdata))
		return "",errors.New(string(tbdata)) 
	}
	return string(tbdata), nil
}

// 发图片消息
func  SendImageMessage(authorizerAccessToken, touser, mediaId string)(string,error){
	serverUrl := fmt.Sprintf(customerURL, authorizerAccessToken)
	client := core.GetPostJSONClient()
	image := NewImage(touser, mediaId, "")
	var rsp core.ErrorResponse
	tbdata, err := client.Excute("POST", serverUrl, image, &rsp)
    if err != nil {
		beego.Critical(err.Error())
		return "", err
	}
	if rsp.ErrCode != 0 {
		beego.Critical("error:", string(tbdata))
		return "",errors.New(string(tbdata)) 
	}
	return string(tbdata), nil
}

// 发送语音消息
func  SendVoiceMessage(authorizerAccessToken, touser, mediaId string)(string,error){
	serverUrl := fmt.Sprintf(customerURL, authorizerAccessToken)
	client := core.GetPostJSONClient()
	voice := NewVoice(touser, mediaId, "")
	var rsp core.ErrorResponse
	tbdata, err := client.Excute("POST", serverUrl, voice, &rsp)
    if err != nil {
		beego.Critical(err.Error())
		return "", err
	}
	if rsp.ErrCode != 0 {
		beego.Critical("error:", string(tbdata))
		return "",errors.New(string(tbdata)) 
	}
	return string(tbdata), nil
}

// 发送视频消息
func  SendVideoMessage(authorizerAccessToken,touser,mediaId,thumbMediaId,title,description string)(string,error){
	serverUrl := fmt.Sprintf(customerURL, authorizerAccessToken)
	client := core.GetPostJSONClient()
	video := NewVideo(touser,mediaId,thumbMediaId,title,description,"")
	var rsp core.ErrorResponse
	tbdata, err := client.Excute("POST", serverUrl, video, &rsp)
    if err != nil {
		beego.Critical(err.Error())
		return "", err
	}
	if rsp.ErrCode != 0 {
		beego.Critical("error:", string(tbdata))
		return "",errors.New(string(tbdata)) 
	}
	return string(tbdata), nil
}

// 发送音乐消息
func  SendMusicMessage(authorizerAccessToken,touser,thumbMediaId,musicURL,hQMusicURL,title,description string)(string,error){
	serverUrl := fmt.Sprintf(customerURL, authorizerAccessToken)
	client := core.GetPostJSONClient()
	music := NewMusic(touser,thumbMediaId,musicURL,hQMusicURL,title,description,"")
	var rsp core.ErrorResponse
	tbdata, err := client.Excute("POST", serverUrl, music, &rsp)
    if err != nil {
		beego.Critical(err.Error())
		return "", err
	}
	if rsp.ErrCode != 0 {
		beego.Critical("error:", string(tbdata))
		return "",errors.New(string(tbdata)) 
	}
	return string(tbdata), nil
}

// 发送外链图文消息
func  SendNewsMessage(authorizerAccessToken,touser string,articles []Article)(string,error){
	serverUrl := fmt.Sprintf(customerURL, authorizerAccessToken)
	client := core.GetPostJSONClient()
	news := NewNews(touser,articles,"")
	var rsp core.ErrorResponse
	tbdata, err := client.Excute("POST", serverUrl, news, &rsp)
    if err != nil {
		beego.Critical(err.Error())
		return "", err
	}
	if rsp.ErrCode != 0 {
		beego.Critical("error:", string(tbdata))
		return "",errors.New(string(tbdata)) 
	}
	return string(tbdata), nil
}

// 发送内链图文消息
func  SendMpNewsMessage(authorizerAccessToken,touser,mediaId string)(string,error){
	serverUrl := fmt.Sprintf(customerURL, authorizerAccessToken)
	client := core.GetPostJSONClient()
	mpnews := NewMPNews(touser,mediaId,"")
	var rsp core.ErrorResponse
	tbdata, err := client.Excute("POST", serverUrl, mpnews, &rsp)
    if err != nil {
		beego.Critical(err.Error())
		return "", err
	}
	if rsp.ErrCode != 0 {
		beego.Critical("error:", string(tbdata))
		return "",errors.New(string(tbdata)) 
	}
	return string(tbdata), nil
}

// 发送卡券消息
func  SendWxCardMessage(authorizerAccessToken,touser,cardId, cardExt string)(string,error){
	serverUrl := fmt.Sprintf(customerURL, authorizerAccessToken)
	client := core.GetPostJSONClient()
	wxCard := NewWxCard(touser, cardId, cardExt, "")
	var rsp core.ErrorResponse
	tbdata, err := client.Excute("POST", serverUrl, wxCard, &rsp)
    if err != nil {
		beego.Critical(err.Error())
		return "", err
	}
	if rsp.ErrCode != 0 {
		beego.Critical("error:", string(tbdata))
		return "",errors.New(string(tbdata)) 
	}
	return string(tbdata), nil
}

// 发送小程序卡片消息
func  SendMiniProgramPageMessage(authorizerAccessToken,touser,title,appid,pagePath,thumbMediaId string)(string,error){
	serverUrl := fmt.Sprintf(customerURL, authorizerAccessToken)
	client := core.GetPostJSONClient()
	miniProgramPage := NewMiniProgramPage(touser,title,appid,pagePath,thumbMediaId,"")
	var rsp core.ErrorResponse
	tbdata, err := client.Excute("POST", serverUrl, miniProgramPage, &rsp)
    if err != nil {
		beego.Critical(err.Error())
		return "", err
	}
	if rsp.ErrCode != 0 {
		beego.Critical("error:", string(tbdata))
		return "",errors.New(string(tbdata)) 
	}
	return string(tbdata), nil
}


